home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Workbench Add-On
/
Workbench Add-On - Volume 1.iso
/
Dev
/
Oberon
/
source
/
Library
/
Conversions.mod
< prev
next >
Wrap
Text File
|
1995-06-29
|
3KB
|
100 lines
(*************************************************************************
$RCSfile: Conversions.mod $
Description: Conversion of basic types to and from strings.
Created by: fjc (Frank Copeland)
$Revision: 1.4 $
$Author: fjc $
$Date: 1995/06/04 23:22:41 $
Copyright © 1994-1995, Frank Copeland.
This file is part of the Oberon-A Library.
See Oberon-A.doc for conditions of use and distribution.
*************************************************************************)
MODULE Conversions;
CONST DIGITS = "0123456789ABCDEF";
VAR digits : ARRAY 17 OF CHAR;
PROCEDURE IntToStr *
( int : LONGINT;
base, field : INTEGER;
padCh : CHAR;
VAR str : ARRAY OF CHAR )
: BOOLEAN;
VAR i, j, k : INTEGER; temp : ARRAY 34 OF CHAR; neg : BOOLEAN;
BEGIN (* IntToStr *)
IF (base < 2) OR (base > 16) OR ~(base IN {2,8,10,16}) THEN
RETURN FALSE
END;
IF field > (LEN (str) - 1) THEN RETURN FALSE END;
IF int < 0 THEN neg := TRUE; IF int # MIN (LONGINT) THEN int := -int END
ELSE neg := FALSE
END;
IF int = MIN (LONGINT) THEN
IF base = 2 THEN temp := "1111111111111111111111111111111"; i := 31
ELSIF base = 8 THEN temp := "77777777771"; i := 11
ELSIF base = 10 THEN temp := "8463847412"; i := 10
ELSIF base = 16 THEN temp := "FFFFFFF7"; i := 8
END
ELSE
i := 0;
REPEAT
temp [i] := digits [SHORT (int MOD base)]; INC (i);
int := int DIV base
UNTIL int = 0;
END;
IF neg & (padCh # "0") THEN neg := FALSE; temp [i] := "-"; INC (i) END;
IF (neg & (i > (LEN (str) - 2))) OR (i > (LEN (str) - 1)) THEN
RETURN FALSE
END;
j := i; k := 0;
IF neg THEN str [k] := "-"; DEC (field); INC (k) END;
WHILE j < field DO str [k] := padCh; INC (j); INC (k) END;
WHILE i > 0 DO DEC (i); str [k] := temp [i]; INC (k) END;
str [k] := 0X;
RETURN TRUE
END IntToStr;
PROCEDURE StrToInt *
( str : ARRAY OF CHAR; base : INTEGER; VAR int : LONGINT )
: BOOLEAN;
VAR i, d, temp, limit : LONGINT; ch : CHAR; neg : BOOLEAN;
<*$CopyArrays-*>
BEGIN (* StrToInt *)
IF (base < 2) OR (base > 16) THEN RETURN FALSE END;
limit := MAX (LONGINT) DIV base; i := 0; ch := str [i];
WHILE (ch # 0X) & (ch <= " ") DO INC (i); ch := str [i] END;
IF ch = "-" THEN neg := TRUE; INC (i); ch := str [i]
ELSE neg := FALSE
END;
temp := 0;
WHILE ch > " " DO
IF (ch >= "0") & (ch <= "9") THEN d := ORD (ch) - ORD ("0")
ELSIF (ch >= "A") & (ch <= "F") THEN d := ORD (ch) - (ORD ("A") - 10)
ELSIF (ch >= "a") & (ch <= "f") THEN d := ORD (ch) - (ORD ("a") - 10)
ELSE RETURN FALSE
END;
IF d >= base THEN RETURN FALSE END;
IF (limit - d) < temp THEN RETURN FALSE END;
temp := temp * base + d;
INC (i); ch := str [i]
END;
IF neg THEN int := -temp ELSE int := temp END;
RETURN TRUE
END StrToInt;
BEGIN
digits := DIGITS
END Conversions.